package com.hhu.wangzb.server.service.impl;

import com.hhu.wangzb.server.service.BranchGwService;
import com.hhu.wangzb.server.service.MainGwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.Query;

import com.hhu.wangzb.server.dao.FieldGroundingDao;
import com.hhu.wangzb.common.entity.FieldGroundingEntity;
import com.hhu.wangzb.server.service.FieldGroundingService;


@Service("fieldGroundingService")
public class FieldGroundingServiceImpl extends ServiceImpl<FieldGroundingDao, FieldGroundingEntity> implements FieldGroundingService {

    @Autowired
    MainGwService mainGwService;

    @Autowired
    BranchGwService branchGwService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FieldGroundingEntity> page = this.page(
                new Query<FieldGroundingEntity>().getPage(params),
                new QueryWrapper<FieldGroundingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {

        // 查询自身
        List<FieldGroundingEntity> fieldGroundingEntities = baseMapper.selectBatchIds(idList);

        for (FieldGroundingEntity entity : fieldGroundingEntities) {
            if (entity.getBgId() != null) branchGwService.removeById(entity.getBgId());
            if (entity.getMgId() != null) mainGwService.removeById(entity.getMgId());
        }

        // 删除自身
        return super.removeByIds(idList);
    }

}